package com.admin.boot.modules.system.controller;


import com.admin.boot.common.utils.Result;
import com.admin.boot.modules.department.entity.SysDepartment;
import com.admin.boot.modules.department.service.ISysDepartmentService;
import com.admin.boot.modules.system.entity.SysRole;
import com.admin.boot.modules.system.entity.SysUser;
import com.admin.boot.modules.system.service.ISysRoleService;
import com.admin.boot.modules.system.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version v3.0
 * @Description: TODO
 * @author: admin-boot
 * @date: 2024-01-15
 */
@Api(tags = "用户表")
@RestController
@RequestMapping("/sysUser/sysUser")
class UserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysDepartmentService sysDepartmentService;

    @GetMapping("/list")
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    public Result<?> list(SysUser sysUser,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        String username = sysUser.getUsername();
        if (StringUtils.isNotBlank(username)){
            queryWrapper.like(SysUser::getUsername, username);
        }
        String role = sysUser.getRoleId();
        if (StringUtils.isNotBlank(role)){
            queryWrapper.eq(SysUser::getRoleId,role);
        }
        String departmentId1 = sysUser.getDepartmentId();
        if (StringUtils.isNotBlank(departmentId1)){
            queryWrapper.eq(SysUser::getDepartmentId,departmentId1);
        }
        Page<SysUser> page = new Page<>(pageNo, pageSize);
        Page<SysUser> sysUserPage = sysUserService.page(page, queryWrapper);
        for (SysUser record : sysUserPage.getRecords()) {
            String roleId = record.getRoleId();
            if (StringUtils.isNotBlank(roleId)){
                SysRole byId = sysRoleService.getById(roleId);
                record.setRoleName(byId!=null? byId.getRoleName() : "");
            }
            String departmentId = record.getDepartmentId();
            if (StringUtils.isNotBlank(departmentId)){
                SysDepartment byId = sysDepartmentService.getById(departmentId);
                record.setDepartmentName(byId!=null?byId.getDepartName():"");
            }
        }
        return Result.OK(sysUserPage);
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    public Result<?> queryList(SysUser sysUser) {
        List<SysUser> list = sysUserService.list(null);
        return Result.OK(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "用户表-添加", notes = "用户表-添加")
    public Result<?> add(@RequestBody SysUser sysUser) {

        sysUserService.save(sysUser);
        return Result.OK("添加成功！");
    }


    @PostMapping("/edit")
    @ApiOperation(value = "用户表-编辑", notes = "用户表-编辑")
    public Result<?> edit(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.OK("编辑成功！");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "用户表-删除", notes = "用户表-删除")
    public Result<?> deleteById(String id) {
        sysUserService.removeById(id);
        return Result.OK("删除成功！");
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "用户表-批量删除", notes = "用户表-批量删除")
    public Result<?> deleteBatch(String ids) {
        for (String id : ids.split(",")) {
            sysUserService.removeById(id);
        }
        return Result.OK("删除成功！");
    }

}
